﻿using B.S.InOut.Data.OutManage;
using B.S.InOut.Domain;
using B.S.InOut.Read.API.Application.Command;
using MediatR;
using NPOI.SS.Formula.Functions;
using System.Drawing.Printing;
using static B.S.InOut.Read.API.Application.Command.DeleteOutManageCommand;

namespace B.S.InOut.Read.API.Application.CommandHandler
{
    // 插入出库信息命令处理程序类，实现 IRequestHandler 接口，处理插入出库信息命令
    public class InsertOutManageCommandHandler : IRequestHandler<InsertOutMenageCommand, int>
    {
        // 产品仓储实例
        private readonly IOutManageRepository _repository;

        // 构造函数，注入出库信息仓储实例
        public InsertOutManageCommandHandler(IOutManageRepository repository)
        {
            _repository = repository;
        }
        public async Task<int> Handle(InsertOutMenageCommand request, CancellationToken cancellationToken)
        {
            // 调用仓储的获取单个出库信息方法
            return await _repository.InsertOutMenageAsync(request.Product);
        }
    }

    // 根据 ID 获取出库信息命令处理程序类，实现 IRequestHandler 接口，处理获取出库信息命令
    public class GetOutManageByIdCommandHandler : IRequestHandler<GetOutMenageByIdCommand, Outmanagement>
    {
        // 产品仓储实例
        private readonly IOutManageRepository _repository;

        // 构造函数，注入出库信息仓储实例
        public GetOutManageByIdCommandHandler(IOutManageRepository repository)
        {
            _repository = repository;
        }
        public async Task<Outmanagement> Handle(GetOutMenageByIdCommand request, CancellationToken cancellationToken)
        {
            // 调用仓储的获取单个出库信息方法
            return await _repository.GetOutMenageByIdAsync(request.Id);
        }
    }

    // 获取所有出库信息命令处理程序类，实现 IRequestHandler 接口，处理获取所有出库信息命令
    public class GetAllOutMenageCommandHandler : IRequestHandler<GetAllOutMenageCommand, IEnumerable<Outmanagement>>
    {
        // 产品仓储实例
        private readonly IOutManageRepository _repository;

        // 构造函数，注入出库信息仓储实例
        public GetAllOutMenageCommandHandler(IOutManageRepository repository)
        {
            _repository = repository;
        }

        // 处理获取所有出库信息命令的异步方法
        public async Task<IEnumerable<Outmanagement>> Handle(GetAllOutMenageCommand request, CancellationToken cancellationToken)
        {
            // 调用仓储的获取所有出库信息方法
            return await _repository.GetAllOutMenageAsync();
        }
    }

    // 更新出库信息命令处理程序类，实现 IRequestHandler 接口，处理更新出库信息命令
    public class UpdateOutMenageCommandHandler : IRequestHandler<UpdateOutMenageCommand, int>
    {
        // 出库信息仓储实例
        private readonly IOutManageRepository _repository;

        // 构造函数，注入出库信息仓储实例
        public UpdateOutMenageCommandHandler(IOutManageRepository repository)
        {
            _repository = repository;
        }

        // 处理更新产品出库信息的异步方法
        public async Task<int> Handle(UpdateOutMenageCommand request, CancellationToken cancellationToken)
        {
            // 调用仓储的更新方法
            return await _repository.UpdateOutMenageAsync(request.Product);
        }
    }

    // 根据 ID 删除出库信息命令处理程序类，实现 IRequestHandler 接口，处理删除出库信息命令
    public class DeleteOutMenageCommandHandler : IRequestHandler<DeleteOutMenageCommand, int>
    {
        // 出库信息仓储实例
        private readonly IOutManageRepository _repository;

        // 构造函数，注入出库信息仓储实例
        public DeleteOutMenageCommandHandler(IOutManageRepository repository)
        {
            _repository = repository;
        }

        // 处理删除出库信息命令的异步方法
        public async Task<int> Handle(DeleteOutMenageCommand request, CancellationToken cancellationToken)
        {
            // 调用仓储的删除方法
            return await _repository.DeleteOutMenageAsync(request.Id);
        }

        /// <summary>
        /// 多条件分页查询产品命令处理程序
        /// </summary>
        public class GetOutMenageByConditionsCommandHandler : IRequestHandler<GetOutMenageByConditionsCommand, IEnumerable<Outmanagement>>
        {
            private readonly IOutManageRepository _repository;

            /// <summary>
            /// 构造函数，注入产品仓储实例
            /// </summary>
            /// <param name="repository">产品仓储实例</param>
            public GetOutMenageByConditionsCommandHandler(IOutManageRepository repository)
            {
                _repository = repository;
            }

            /// <summary>
            /// 处理多条件分页查询产品命令的异步方法
            /// </summary>
            /// <param name="request">多条件分页查询产品命令</param>
            /// <param name="cancellationToken">取消令牌</param>
            /// <returns>分页后的列表</returns>

            public async Task<IEnumerable<Outmanagement>> Handle(GetOutMenageByConditionsCommand request, CancellationToken cancellationToken)
            {
                return await _repository.GetOutMenageByConditionsAsync(request.Outboundnumber, request.Outbound, request.OutAuditstatus, request.pageindex, request.pageSize);
            }

            /// <summary>
            /// 批量删除产品命令处理程序
            /// </summary>
            public class DeleteOutManageCommandHandler : IRequestHandler<DeleteOutManageCommand, int>
            {
                private readonly IOutManageRepository _repository;

                /// <summary>
                /// 构造函数，注入产品仓储实例
                /// </summary>
                /// <param name="repository">产品仓储实例</param>
                public DeleteOutManageCommandHandler(IOutManageRepository repository)
                {
                    _repository = repository;
                }

                /// <summary>
                /// 处理批量删除产品命令的异步方法
                /// </summary>
                /// <param name="request">批量删除产品命令</param>
                /// <param name="cancellationToken">取消令牌</param>
                /// <returns>受影响的行数</returns>
                public async Task<int> Handle(DeleteOutManageCommand request, CancellationToken cancellationToken)
                {
                    return await _repository.DeleteOutMenageAsyncd(request.ProductIds);
                }
            }
        }
        /// <summary>
        /// 处理联合查询 Outmanagement 和 ProductModel 表的命令处理程序
        /// </summary>
        public class GetOutmanagementWithProductModelCommandHandler : IRequestHandler<GetOutmanagementWithProductModelCommand, IEnumerable<OutmanagementProduct>>
        {
            private readonly IOutManageRepository _repository;

            public GetOutmanagementWithProductModelCommandHandler(IOutManageRepository repository)
            {
                _repository = repository;
            }

            public async Task<IEnumerable<OutmanagementProduct>> Handle(GetOutmanagementWithProductModelCommand request, CancellationToken cancellationToken)
            {
                // 调用仓储层的联合查询方法
                return await _repository.GetOutmanagementWithProductModelAsync(request.Outboundnumber, request.Outbound,request.OutAuditstatus, request.pageindex,request.pageSize);
            }
        }





        /// <summary>
        /// 处理根据出库 ID 查询 Outmanagement 和 ProductModel 两表关联数据的命令处理程序
        /// </summary>
        public class GetOutmanagementAndProductModelByIdCommandHandler : IRequestHandler<GetOutmanagementAndProductModelByIdCommand, OutmanagementProduct>
        {
            private readonly IOutManageRepository _repository;

            public GetOutmanagementAndProductModelByIdCommandHandler(IOutManageRepository repository)
            {
                _repository = repository;
            }

            public async Task<OutmanagementProduct> Handle(GetOutmanagementAndProductModelByIdCommand request, CancellationToken cancellationToken)
            {
                // 调用仓储层的根据 ID 查询方法
                return await _repository.GetOutmanagementAndProductModelByIdAsync(request.Id);
            }
        }

        /// <summary>
        /// 根据出库单编号查询
        /// </summary>
        public class GetOutmanagemenByIdCommandHandler : IRequestHandler<GetOutmanagemenByIdCommand, IEnumerable<Outmanagement>>
        {
            private readonly IOutManageRepository _repository;

            public GetOutmanagemenByIdCommandHandler(IOutManageRepository repository)
            {
                _repository = repository;
            }

            public async Task<IEnumerable<Outmanagement>> Handle(GetOutmanagemenByIdCommand request, CancellationToken cancellationToken)
            {
                return await _repository.GetOutmanagementByOutboundNumberAsync(request.Outboundnumber);
            }
        }
    }
}
